#include "StdAfx.h"
#include "ReplaceSpace.h"


ReplaceSpace::ReplaceSpace(void)
{
	char* setChar = "Happy World!";
	replaceSpace(setChar,15);
}


ReplaceSpace::~ReplaceSpace(void)
{
}

void ReplaceSpace::replaceSpace(char* setString,int length){
	int origLength = 0;
	int spaceLength = 0;

	if(setString == NULL){
		return;
	}

	while(setString[origLength]!= '\0'){
		origLength++;

		if(setString[origLength] == ' '){
			spaceLength++;
		}
	}
	cout<<"orighLength="<<origLength<<";spaceLength="<<spaceLength<<endl;
	
	int targetLength = origLength+spaceLength*2;
	if(targetLength>length){
		return;
	}

	char* newString = new char[targetLength];

	while(origLength>=0){
		if(setString[origLength] == ' '){
			newString[targetLength--] = '0';
			newString[targetLength--] = '2';
			newString[targetLength--] = '%';
			
		}else{
			newString[targetLength--] = setString[origLength];
		}
		origLength--;
	}
	cout<<"over:newString="<<newString<<endl;
	
	//oldString2[13]="123123";
}
